//
//  EGgoodsCell.m
//  Orchard
//
//  Created by ios001 on 16/8/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGgoodsCell.h"

@interface EGgoodsCell()

@property (nonatomic,strong) UIView      *addressView;

//@property (nonatomic,strong) UILabel     *goodsLabel;

@property (nonatomic,strong) UILabel     *goodsLabel;

@property (nonatomic,strong) UIButton    *addButton;

@property (nonatomic,strong) UIButton    *reduceButton;


@property (nonatomic,strong) UILabel     *goodsNumLabel;

@property (nonatomic,assign) NSInteger   goodsNum;
@end

@implementation EGgoodsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.goodsNum = 10;

        [self setupUI];
        self.backgroundColor = RANDOMCOLOR;
        

    }
    return self;
}

- (void)layoutSubviews{
    [self setLayout];
}



- (void)setupUI{
    [self.contentView addSubview:self.addressView];
    [self.addressView addSubview:self.goodsLabel];
    [self.addressView addSubview:self.addButton];
    [self.addButton addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.addressView addSubview:self.goodsNumLabel];
    [self.addressView addSubview:self.reduceButton];
    [self.reduceButton addTarget:self action:@selector(reduceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (IBAction)addBtnClick:(id)sender{
    self.goodsNum++;
    self.goodsNumLabel.text = [NSString stringWithFormat:@"%ld",self.goodsNum];
}

- (IBAction)reduceBtnClick:(id)sende{
    if (_goodsNum <= 1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"hiddenVc" object:self];
        return;
    }
    self.goodsNum--;
    self.goodsNumLabel.text = [NSString stringWithFormat:@"%ld",self.goodsNum];
}



- (void)setLayout{
    
    [self.addressView makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.leading.trailing.equalTo(self.contentView);
    }];
    
    
    [self.goodsLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.addressView).offset(10);
        make.centerY.equalTo(self.addressView);
    }];
    
    [self.addButton makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.addressView).offset(-5);
        make.centerY.equalTo(self.addressView);
    }];
    
    [self.goodsNumLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.addressView);
        make.trailing.equalTo(self.addButton.leading).offset(-5);
    }];
    
    [self.reduceButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.addressView);
        make.trailing.equalTo(self.goodsNumLabel.leading).offset(-5);
    }];
}


- (UIView *)addressView{
    if (_addressView == nil) {
        _addressView = [[UIView alloc] init];
    }
    return _addressView;
}

- (UILabel *)goodsLabel{
    if (_goodsLabel == nil) {
        _goodsLabel = [[UILabel alloc] init];
//        _goodsLabel.text = self.testModel.name;
        _goodsLabel.font = [UIFont systemFontOfSize:15];
    }
    return _goodsLabel;
}

- (UIButton *)addButton{
    if (_addButton == nil) {
        _addButton = [[UIButton alloc] init];
        [_addButton setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    }
    return _addButton;
}

- (UILabel *)goodsNumLabel{
    if (_goodsNumLabel == nil) {
        _goodsNumLabel = [[UILabel alloc] init];
        _goodsNumLabel.text = [NSString stringWithFormat:@"%ld",self.goodsNum];
        _goodsNumLabel.font = [UIFont systemFontOfSize:15];
    }
    return _goodsNumLabel;
}

- (UIButton *)reduceButton{
    if (_reduceButton == nil) {
        _reduceButton = [[UIButton alloc] init];
        [_reduceButton setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    }
    return _reduceButton;
}



@end
